{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "63107b1a",
   "metadata": {},
   "source": [
    "# 通用文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5a553d2f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.bc3b3c49636c864450fc00c9fccaad41.315360000.1965038092.282335-25946130', 'expires_in': 2592000, 'session_key': '9mzdWBGZy1Rv1OgC/fFslxGCawHMWBIf4RPxkmrLxP/xZtgDnu/Tu1xVey30kKARQj1m3jRrw3sZaa/xxNZ9Ap8lTQ8TwQ==', 'access_token': '24.cada1707d83edd2088283dcc90bfbc63.2592000.1652270092.282335-25946130', 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': 'fd5aaa0a927bd21b00cb3f3ee8144b94'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=HkvUwkexVBVW8S1HM9nipehM&client_secret=0flTgAXGgrE25Za65rHax1baGyFcvbm7'\n",
    "# 准备url\n",
    "payload={\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'g0yxhKEsWIllgCwScDbGGOrR',\n",
    "    'client_secret':'iR843eA9RMII1RjqPoUpSc4hqeXPAuAr'\n",
    "    \n",
    "}\n",
    "  \n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "785dd065",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.cada1707d83edd2088283dcc90bfbc63.2592000.1652270092.282335-25946130'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token=response.json()[\"access_token\"]\n",
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b7c2f66f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '波'}, {'words': '吐司版低卡芋泥蛋糕'}, {'words': '控卡低糖'}, {'words': '好吃不'}], 'words_result_num': 4, 'log_id': 1513485743548378228}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版）\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('wenzi.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8c478ccd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '吐司版低卡芋泥蛋糕'}, {'words': '控卡低糖'}, {'words': '好吃不胖'}], 'words_result_num': 3, 'log_id': 1513486364887396679}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "网络图片文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('wenzi.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "688b2ece",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'codes_result': [{'text': ['https://github.com/sylnsfar/qrcode'], 'type': 'QR_CODE'}, {'text': ['https://github.com/sylnsfar/qrcode'], 'type': 'QR_CODE'}], 'codes_result_num': 2, 'log_id': 1513488561230145192}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "二维码识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('erweima.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "38ae00ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': {'姓名': {'location': {'top': 434, 'left': 669, 'width': 276, 'height': 137}, 'words': '徐乐'}, '民族': {'location': {'top': 664, 'left': 1153, 'width': 98, 'height': 110}, 'words': '汉'}, '住址': {'location': {'top': 1075, 'left': 643, 'width': 1063, 'height': 249}, 'words': '安徽省宿州市埇桥区朱仙庄镇'}, '公民身份号码': {'location': {'top': 1598, 'left': 1063, 'width': 1406, 'height': 144}, 'words': '652901196611026716'}, '出生': {'location': {'top': 861, 'left': 641, 'width': 848, 'height': 110}, 'words': '19661102'}, '性别': {'location': {'top': 661, 'left': 651, 'width': 103, 'height': 114}, 'words': '男'}}, 'words_result_num': 6, 'idcard_number_type': 1, 'image_status': 'normal', 'log_id': 1513489331977190066}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "身份证识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('shenfen.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"id_card_side\":\"front\",\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e73e44e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result_num': 43, 'words_result': {'AmountInWords': '玖佰零肆圆整', 'InvoiceNumConfirm': '02802144', 'CommodityEndDate': [], 'CommodityVehicleType': [], 'CommodityStartDate': [], 'CommodityPrice': [{'row': '1', 'word': '230.08849558'}, {'row': '2', 'word': '8.407079646'}, {'row': '3', 'word': '54.867256637'}, {'row': '4', 'word': '6.1946902655'}], 'NoteDrawer': '李军', 'SellerAddress': '安徽省芜湖市镀湖区大嘉社区居民委商金办公楼212室0553-3816558', 'CommodityNum': [{'row': '1', 'word': '1'}, {'row': '2', 'word': '40'}, {'row': '3', 'word': '2'}, {'row': '4', 'word': '20'}], 'SellerRegisterNum': '91340200MA2NTBJD7W', 'MachineCode': '', 'Remarks': '', 'SellerBank': '芜湖扬子农村商业银行股份有限公司20000547260610500000067', 'CommodityTaxRate': [{'row': '1', 'word': '13%'}, {'row': '2', 'word': '13%'}, {'row': '3', 'word': '13%'}, {'row': '4', 'word': '13%'}], 'TotalTax': '104.01', 'InvoiceCodeConfirm': '034022000104', 'CheckCode': '82322969860869404193', 'InvoiceCode': '034022000104', 'InvoiceDate': '2021年06月18日', 'PurchaserRegisterNum': '113402000030106774', 'InvoiceTypeOrg': '安徽增值税普通发票', 'OnlinePay': '', 'Password': '8/54+41-7//<*>-8>096<1853*3-98765-49716-/9/09-0>37-<16802*656421<192/3*>2</2*+3*-7093*-+765-79716+-9**9*66+6', 'Agent': '否', 'AmountInFiguers': '904.00', 'PurchaserBank': '', 'Checker': '', 'City': '芜湖', 'TotalAmount': '799.99', 'CommodityAmount': [{'row': '1', 'word': '230.09'}, {'row': '2', 'word': '336.28'}, {'row': '3', 'word': '109.73'}, {'row': '4', 'word': '123.89'}], 'PurchaserName': '芜湖市人民防空办公室', 'CommodityType': [], 'Province': '安徽', 'InvoiceType': '普通发票', 'SheetNum': '第二联：发票联', 'PurchaserAddress': '', 'CommodityTax': [{'row': '1', 'word': '29.91'}, {'row': '2', 'word': '43.72'}, {'row': '3', 'word': '14.27'}, {'row': '4', 'word': '16.11'}], 'CommodityPlateNum': [], 'CommodityUnit': [{'row': '1', 'word': '只'}, {'row': '2', 'word': '只'}, {'row': '3', 'word': '只'}, {'row': '4', 'word': '包'}], 'Payee': '', 'CommodityName': [{'row': '1', 'word': '*计算机配套产品*兄弟粉盒'}, {'row': '2', 'word': '*文具*档案盒'}, {'row': '3', 'word': '*计算机配套产品*USB网卡'}, {'row': '4', 'word': '*文具*抽杆夹'}], 'SellerName': '芜湖昶隆办公设备销售有限责任公司', 'InvoiceNum': '02802144'}, 'log_id': 1513490304432501712}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "增值税发票识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('zen.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2530c42",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
